'''
  @Description      
  @auther         leizi
  @create          2020-02-16 15:51
'''
import os, subprocess
from until.log import LOG


def write_file(filename, content, is_cover=False):
    '''
    写入文件 覆盖写入
    '''
    try:
        newstr = ""
        if isinstance(content, list or tuple):
            for str in content:
                newstr = newstr + str + "\n"
        else:
            newstr = content
        if is_cover == True:
            file_mode = "wb"
        else:
            file_mode = "ab"
        with open(filename, file_mode) as f_w:
            f_w.write(newstr)
    except Exception as e:
        LOG.info("写入文件失败，原因：%s" % str(e))


def del_files(filename):
    '''
    删除文件
    '''
    try:
        if os.path.exists(filename):
            subprocess.call("rm -rf {}".format(filename), shell=True)
    except Exception as e:
        LOG.info("删除文件失败，原因：%s" % str(e))


def mk_dir(foldername):
    '''
    创建文件目录
    '''
    try:
        if not os.path.exists(foldername):
            subprocess.call("mkdir {}".format(foldername), shell=True)
    except Exception as e:
        LOG.info("创建文件目录失败，原因：%s" % str(e))


def read_file(filename):
    '''
    读取文件
    '''
    result = ''
    try:
        with open(filename, "r") as f_r:
            result = f_r.readlines()
    except Exception as e:
        LOG.info("读取文件失败，原因：%s" % str(e))
    finally:
        if isinstance(result, list) and len(result) == 1:
            return result[0]
        else:
            return result
